package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.RoleDao;
import com.itheima.domain.Role;
import com.itheima.service.RoleService;
import com.itheima.utils.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.UUID;
import java.util.stream.Stream;

public class RoleServiceImpl implements RoleService {
    @Override
    public PageInfo<Role> findByPage(int currentPage, int pageSize) {
        PageInfo<Role> page = null;
        SqlSession sqlSession = null;
        try {
            //1 通过MybatisUtil工厂类获取SqlSession对象
            sqlSession = MybatisUtil.getSqlSession();
            //2 通过MybatisUtil工厂类获取dao接口的代理对象
            RoleDao mapper = MybatisUtil.getMapper(sqlSession, RoleDao.class);
            //3 执行操作释放资源
            //3.1 在查询所有之前设置分页参数
            PageHelper.startPage(currentPage,pageSize);
            List<Role> list = mapper.findAll();
             page = new PageInfo<>(list);
            //MybatisUtil.commit(sqlSession);
        } finally {
            MybatisUtil.close(sqlSession);
        }
        return page;
    }

    @Override
    public void save(Role cept) {
        SqlSession sqlSession=null;
        try {
             sqlSession = MybatisUtil.getSqlSession();
            RoleDao mapper = MybatisUtil.getMapper(sqlSession, RoleDao.class);
            //生成一个唯一的id，保存到cept
            String id = UUID.randomUUID().toString().replace("-", "");
            cept.setId(id);
            mapper.save(cept);
            MybatisUtil.commit(sqlSession);

        } finally {
            MybatisUtil.close(sqlSession);
        }
    }

    @Override
    public Role findById(String id) {
        Role cept = null;
        SqlSession sqlSession=null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            RoleDao mapper = MybatisUtil.getMapper(sqlSession, RoleDao.class);

            cept =  mapper.findById(id);
            MybatisUtil.commit(sqlSession);

        } finally {
            MybatisUtil.close(sqlSession);
        }
        return cept;
    }

    @Override
    public void update(Role cept) {
        SqlSession sqlSession=null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            RoleDao mapper = MybatisUtil.getMapper(sqlSession, RoleDao.class);

            mapper.update(cept);
            MybatisUtil.commit(sqlSession);

        } finally {
            MybatisUtil.close(sqlSession);
        }
    }

    @Override
    public void delete(String[] ids) {
        SqlSession sqlSession=null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            RoleDao mapper = MybatisUtil.getMapper(sqlSession, RoleDao.class);

            Stream.of(ids).forEach(mapper::delete);
            MybatisUtil.commit(sqlSession);

        } finally {
            MybatisUtil.close(sqlSession);
        }
    }

    @Override
    public List<Role> findAll() {
       List<Role>  list = null;
        SqlSession sqlSession=null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            RoleDao mapper = MybatisUtil.getMapper(sqlSession, RoleDao.class);

            list =  mapper.findAll();
            MybatisUtil.commit(sqlSession);

        } finally {
            MybatisUtil.close(sqlSession);
        }
        return list;
    }

    @Override
    public void updateRoleAndModules(String roleId, String[] moduleIds) {
        SqlSession sqlSession=null;
        try {
            //1 通过MybatisUtil工厂类获取SqlSession对象
            sqlSession = MybatisUtil.getSqlSession();
            //2 通过MybatisUtil工厂类获取dao接口的代理对象
            RoleDao mapper = MybatisUtil.getMapper(sqlSession, RoleDao.class);
            //3 执行操作
            //3.1 删除角色绑定的所有模块，操作ss_role_module
            mapper.deleteModulesByRoleId(roleId);
            //3.2 遍历moduleIds，给角色绑定新的module，操作ss_role_module
            Stream.of(moduleIds).forEach(moduleId->
                mapper.saveModulesByRoleId(roleId,moduleId)
            );
            //4 提交事务
            MybatisUtil.commit(sqlSession);
        } finally {
            //释放资源
            MybatisUtil.close(sqlSession);
        }
    }

    @Override
    public String[] findRoleIdsByUserId(String userId) {
        SqlSession sqlSession=null;
        String[] checkedRoleIds=null;
        try {
            //1 通过MybatisUtil工厂类获取SqlSession对象
            sqlSession = MybatisUtil.getSqlSession();
            //2 通过MybatisUtil工厂类获取dao接口的代理对象
            RoleDao mapper = MybatisUtil.getMapper(sqlSession, RoleDao.class);
            //3 执行操作
            //3.1 删除角色绑定的所有模块，操作ss_role_module
            checkedRoleIds =   mapper.findRoleIdsByUserId(userId);

            MybatisUtil.commit(sqlSession);
        } finally {
            //释放资源
            MybatisUtil.close(sqlSession);
        }
        return checkedRoleIds;
    }
}
